package leetcode.code0142;

import leetcode.helper.HelpDebug;
import leetcode.helper.tree.ListNode;

public class Solution142 {

	public ListNode detectCycle(ListNode head) {
		ListNode fast = head;
		ListNode slow = head;
		boolean has = false;
		while (fast != null && fast.next != null) {
			slow = slow.next;
			fast = fast.next.next;
			if (slow == fast) {
				has = true;
				break;
			}
		}
		if (!has) {
			return null;
		}
		slow = head;
		while (slow != fast) {
			slow = slow.next;
			fast = fast.next;
		}
		return slow;
	}

	public static void main(String[] args) {
		ListNode head = ListNode.set(HelpDebug.str2array3("[1]"));
		Solution142 so = new Solution142();
		so.detectCycle(head);
	}

}
